// import './utils/logger.js';
import {chadanJob, refreshToken} from './jobs/chadan.js';
import schedule from "node-schedule";
// import config from "./config.js";
import { sleep } from "./utils/index.js";

await refreshToken();

async function job() {
  let count = 20;
  const key = Date.now().toString();
  for (let i = 0; i < count; i++) {
    setTimeout(() => {
      console.log(`${i} ${new Date().toISOString()}`);
      console.time(`${i}-${key}`)
      chadanJob().catch(console.error)
        .finally(()=>{
          console.timeEnd(`${i}-${key}`)
        });
    },i * 10)
  }
}

schedule.scheduleJob('9,19,29,39,49,59 * * * * ?', async function(){
  console.log('----------------------------------------------------------------------------------------------------------------------');
  await sleep(990);
  try {
    await job();
    await sleep(5 * 1000);
    await refreshToken();
  }catch (e) {
    console.error(e.message);
  }
});
